/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.mcx.modules.service.impl;

import com.mcx.utils.QueryHelp;
import com.mcx.modules.domain.Gameserver;
import com.mcx.modules.repository.GameserverRepository;
import com.mcx.modules.service.GameserverService;
import com.mcx.modules.service.dto.GameserverQueryCriteria;
import com.mcx.modules.service.mapstruct.GameserverMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

@Service
@RequiredArgsConstructor
public class GameserverServiceImpl implements GameserverService {

    private final GameserverRepository gameserverRepository;
    private final GameserverMapper gameserverMapper;

    @Override
    public Object queryAll(GameserverQueryCriteria criteria, Pageable pageable) {
        Page<Gameserver> page = gameserverRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder), pageable);
        return page;
    }

    @Override
    public List<Gameserver> queryAll(GameserverQueryCriteria criteria){
        return gameserverRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder));

    }

    @Override
    @Transactional
    public Gameserver findById(Integer id) {
        Gameserver gameserver = gameserverRepository.findById(id).orElseGet(Gameserver::new);
        return gameserver;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Gameserver create(Gameserver resources) {
        return gameserverRepository.save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Gameserver resources) {
        Gameserver gameserver = gameserverRepository.findById(resources.getId()).orElseGet(Gameserver::new);
        gameserver.copy(resources);
        gameserverRepository.save(gameserver);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            gameserverRepository.deleteById(id);
        }
    }

    @Override
    public Object getPage() {
        ArrayList<HashMap<String, String>> pages = new ArrayList<HashMap<String, String>>();
        int total = (int)gameserverRepository.count();
        int pageCount = total / 10;
        int last = total % 10;
        for(int i=1; i<=pageCount; i++)
        {
            HashMap<String, String> page = new HashMap<String, String>();
            page.put("PageIndex", ""+(i-1));
            page.put("Name", ((i-1)*10+1)+"-"+i+"0服");
            pages.add(page);
        }
        if (last!=0) {
            HashMap<String, String> page = new HashMap<String, String>();
            page.put("PageIndex", ""+(pageCount));
            page.put("Name", (pageCount*10+1)+"-"+(pageCount*10+last)+"服");
            pages.add(page);
        }
        Collections.reverse(pages);
        return pages;
    }

    public Integer getCount() {
        return (int)gameserverRepository.count();
    }
}